﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Security.Principal;
using System.Security.AccessControl;

namespace HU8.Helpers.Files.Resource
{
    /// <summary>
    /// 文件夹
    /// </summary>
    public class Folder : ResourceItem
    {
        public Folder(string path, string basePath, string lang = "zh") : base()
        {
            Exists = Directory.Exists(path);
            if (Exists)
            {
                DirectoryInfo di = new DirectoryInfo(path);
                Name = di.Name;
                Path = di.FullName;
                if (basePath.Length > 0)
                {
                    RelativePath = path.Replace(basePath, "~/").Replace("\\", "/");
                }
                try
                {
                    //Size = FileHelper.GetDirectorySize(di.FullName); //占用太多CPU
                    FileCount = di.GetFiles().Count();
                    FolderCount = di.GetDirectories().Count();
                }
                catch (Exception ex)
                {
                }
                Created = di.CreationTime;
                LastModified = di.LastWriteTime;
                LastAccessed = di.LastAccessTime;
                Permission = GetPermission(lang);

                Attributes = di.Attributes.ToString();
                IsReadOnly = ((di.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
                IsHidden = ((di.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden);
                IsArchive = ((di.Attributes & FileAttributes.Archive) == FileAttributes.Archive);
                IsSystem = ((di.Attributes & FileAttributes.System) == FileAttributes.System);
                LoadSecurityInfo();
            }
            else
            {
                ErrorOccured = true;
                switch (lang)
                {
                    case "zh":
                        Error = "请求的文件夹不存在";
                        break;
                    default:
                        Error = "The directory you tried to access does not exist.";
                        break;
                }
            }
        }
        public Folder() { }


        public int FileCount { get; set; } //文件数量
        public int FolderCount { get; set; } //文件夹数量
        
        public List<Folder> Folders { get; set; }
        public List<FolderFile> Files { get; set; }

        public bool ErrorOccured { get; set; } 
        public string Error { get; set; }
    }
}